Better respect for waypoint const-ness. From Alex.
authorrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Fri, 20 Sep 2002 17:47:46 +0000 (17:47 +0000)
committerrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Fri, 20 Sep 2002 17:47:46 +0000 (17:47 +0000)
gpsbabel/csv.c
gpsbabel/csv_util.c
gpsbabel/csv_util.h
gpsbabel/mxf.c
gpsbabel/ozi.c
gpsbabel/psp.c

index e2b74404fbfda16704f3bf43ba61d0a6beca266c..f7c70cd32801495539d8d2ace152154ee4c46c46 100644 (file)
@@ -72,12 +72,12 @@ data_read(void)
                linecount++;
                memset(&buff, '\0', sizeof(buff));
                fgets(buff, sizeof(buff), file_in);
-
+                 
                if (strlen(buff)) {
 
                    wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1);
-
                    s = buff;
+
                    /* data delimited by commas, not enclosed */
                    s = csv_lineparse(s, ",", "", linecount);
 
@@ -92,8 +92,7 @@ data_read(void)
                                wpt_tmp->position.longitude.degrees = atof(s);
                                break;
                        case 2:
-                               wpt_tmp->description = xstrdup(s);
-                               wpt_tmp->description = csv_stringtrim(wpt_tmp->description, " ");
+                               wpt_tmp->description = csv_stringtrim(s, " ");
                                break;
                        default:
                                fprintf (stderr, "%s: Warning: unmapped data fields on line %d.\n", 
@@ -106,6 +105,10 @@ data_read(void)
                }
            
                wpt_tmp->creation_time = time(NULL);
+               
+               /* We'll make up our own shortname. */
+               wpt_tmp->shortname = mkshort(wpt_tmp->description);
+               
                waypt_add(wpt_tmp);
 
        } else {
@@ -116,30 +119,33 @@ data_read(void)
 }
 
 static void
-gpsutil_disp(waypoint *wpt)
+csv_waypt_pr(const waypoint *wpt)
 {
        double lon,lat;
+       char * description = NULL;
 
        lon = wpt->position.longitude.degrees;
        lat = wpt->position.latitude.degrees;
 
         if (wpt->description) 
-           wpt->description = csv_stringclean(wpt->description, ",\"");
+           description = csv_stringclean(wpt->description, ",\"");
 
        fprintf(file_out, "%08.5f, %08.5f, %s\n",
                lat,
                lon,
-               wpt->description);
+               description);
+               
+       if (description)
+               free (description);
 
 }
 
 static void
 data_write(void)
 {
-       waypt_disp_all(gpsutil_disp);
+       waypt_disp_all(csv_waypt_pr);
 }
 
-
 ff_vecs_t csv_vecs = {
        rd_init,
        wr_init,
index fe0b440b081d6dc6428d4c7077d058e18cdb9768..8f27c5f2fc7a8805bb0c00ca62156a7c5d7436a7 100644 (file)
 
 /*********************************************************************/
 /* csv_stringclean() - remove any unwanted characters from string.   */
-/*                     returns MODIFIED string.                      */
-/*     usage: csv_stringclean(stringtoclean, "&,\"")                 */
+/*                     returns copy of string.                       */
+/*     usage: p = csv_stringclean(stringtoclean, "&,\"")             */
 /*            (strip out ampersands, commas, and quotes.             */
 /*********************************************************************/
 char *
-csv_stringclean(char *string, const char *chararray) {
-    char * p;
-    char * lp;
+csv_stringclean(const char *string, const char *chararray) {
+    char * p1;
+    char * p2;
     const char * cp;
+    char * tmp = xstrdup(string);
 
     if ((! string) || (! chararray)) {
-        return (string);
+        return (tmp);
     }
-    
-    /* lp - end of the original string */
-    lp = string;
-    while (*lp) lp++;
+
+    /* p2 - end of the original string */
+    p2 = tmp;
+    while (*p2) p2++;
     
     cp = chararray;
 
     while (*cp) {
-        p = string;
-        while (*p) {
-            if (*cp == *p) {
+        p1 = tmp;
+        while (*p1) {
+            if (*cp == *p1) {
                 /* we don't want this character! */
-                strncpy(p, p+1, (lp - p));
+                strncpy(p1, p1 + 1, (p2 - p1));
+                p1[p2 - p1] = '\0';
             }
-            p++;
+            p1++;
         }
         cp++;
     }
     
-    return (string);
+    return (tmp);
 }
 
 /***********************************************************************************/
 /* csv_stringtrim() - trim whitespace and leading and trailing enclosures (quotes) */
-/*                    returns MODIFIED string.                                     */
-/*    usage: csv_stringtrim(string, "\"")                                          */
+/*                    returns a copy of the modified string                        */
+/*    usage: p = csv_stringtrim(string, "\"")                                      */
 /***********************************************************************************/
 char *
-csv_stringtrim(char *string, const char *enclosure)
+csv_stringtrim(const char *string, const char *enclosure)
 {
-    static char *p1 = NULL;
+    static const char *p1 = NULL;
     char *p2 = NULL;
+    char * tmp = xstrdup(string);
     size_t elen;
 
-    if (!string) {
-       return (string);
+    if (!strlen(string)) {
+       return (tmp);
     }
 
     if (!enclosure) {
@@ -85,53 +88,53 @@ csv_stringtrim(char *string, const char *enclosure)
        elen = strlen(enclosure);
     }
 
-    p2 = string;
+    p2 = tmp;
+    p1 = tmp;
 
-    /* advance pointer to the end of the string */
-    while ((*p2) && (p2++)) {
-    }
+    /* advance p2 to the end of the string, then back it off. */
+    while ((*p2) && (p2++)) { }
     p2--;
 
     /* trim off trailing whitespace */
     while (isspace(*p2)) {
-       *p2 = '\0';
        p2--;
     }
 
-    p1 = string;
-
     /* advance p1 past any leading whitespace */
     while (isspace(*p1)) {
        p1++;
     }
 
-    /* if we have enclosures, yank them out in pairs */
+    /* if we have enclosures, skip past them in pairs */
     if (elen) {
-       while ((strncmp(p1, enclosure, elen) == 0)
+       while ((strncmp(tmp, enclosure, elen) == 0)
               && (strncmp(p2, enclosure, elen) == 0)) {
-           *p2 = '\0';
-           p2--;
-           p1++;
+           p2 -= elen;
+            p1 += elen;
        }
     }
 
-    return (p1);
+    /* copy what's left over back into tmp. */
+    strncpy(tmp, p1, (p2 - p1) + 1);
+    tmp[(p2 - p1) + 1] = '\0';
+
+    return (tmp);
 }
 
 /*****************************************************************************/
 /* csv_lineparse() - extract data fields from a delimited string. designed   */
 /*                   to handle quoted and delimited data within quotes.      */
 /*                   returns temporary COPY of delimited data field (use it  */
-/*                   or lose it).                                            */
+/*                   or lose it on the next call).                           */
 /*    usage: p = csv_lineparse(string, ",", "\"", line)  [initial call]      */
 /*           p = csv_lineparse(NULL, ",", "\"", line)    [subsequent calls]  */
 /*****************************************************************************/
 char *
-csv_lineparse(char *stringstart, const char *delimited_by, 
+csv_lineparse(const char *stringstart, const char *delimited_by, 
                const char *enclosed_in, const int line_no)
 {
-    char *sp;
-    static char *p = NULL;
+    const char *sp;
+    static const char *p = NULL;
     static char *tmp = NULL;
     size_t dlen, elen;
     int enclosedepth = 0;
@@ -181,6 +184,7 @@ csv_lineparse(char *stringstart, const char *delimited_by,
     tmp = xcalloc((p - sp) + 1, sizeof(char));
 
     strncpy(tmp, sp, (p - sp));
+    tmp[p - sp] = '\0'; 
 
     if (dfound) {
        /* skip over the delimited_by */
index 170cc43b57894a73a1d814d2c548d903f4a32121..5d2660bf8ddff08432aecc774ae996ed002da185 100644 (file)
 
 /* function prototypes */
 char *
-csv_stringtrim(char *string, const char *enclosure);
+csv_stringtrim(const char *string, const char *enclosure);
 
 char *
-csv_lineparse(char *stringstart, const char *delimited_by, const char *enclosed_in, const int line_no);
+csv_lineparse(const char *stringstart, const char *delimited_by, const char *enclosed_in, const int line_no);
 
 char *
-csv_stringclean(char *string, const char *chararray);
+csv_stringclean(const char *string, const char *chararray);
index ca062349e7d9fcfedf76248df324a5c3a58baeb7..e5399b4673cb52dded238c2f9dc0ec817012e86c 100644 (file)
@@ -87,8 +87,9 @@ data_read(void)
 
            /* data delimited by commas, possibly enclosed in quotes.  */
            s = buff;
-           s = csv_lineparse(s, ",", "\"", linecount);
 
+           s = csv_lineparse(s, ",", "\"", linecount);
+           
            i = 0;
            while (s) {
                switch (i) {
@@ -99,12 +100,10 @@ data_read(void)
                    wpt_tmp->position.longitude.degrees = atof(s);
                    break;
                case 2:
-                   wpt_tmp->description = xstrdup(s);
-                   wpt_tmp->description = csv_stringtrim(wpt_tmp->description, "");
+                   wpt_tmp->description = csv_stringtrim(s, "");
                    break;
                case 3:
-                   wpt_tmp->shortname = xstrdup(s);
-                   wpt_tmp->shortname = csv_stringtrim(wpt_tmp->shortname, "");
+                   wpt_tmp->shortname = csv_stringtrim(s, "");
                    break;
                case 4:
                     /* ignore.  another name-type  */
@@ -142,27 +141,42 @@ data_read(void)
 }
 
 static void 
-mxf_disp(waypoint * wpt)
+mxf_waypt_pr(const waypoint * wpt)
 {
     int icon = 47; /* default to "dot" */
     const char *color_hex = "ff0000";
-
-    csv_stringclean(wpt->shortname, ",\"");
-    wpt->shortname = csv_stringtrim(wpt->shortname, "");
+    char *shortname = NULL;
+    char *description = NULL;
+
+    if (wpt->shortname) {
+        shortname = csv_stringclean(wpt->shortname, ",\"");
+        shortname = csv_stringtrim(shortname, "");
+    } else {
+        shortname = xstrdup("");
+    }
+    
+    if (wpt->description) {
+        description = csv_stringclean(wpt->description, ",\"");
+        description = csv_stringtrim(description, "");
+    } else {
+        shortname = xstrdup("");
+    }
     
-    csv_stringclean(wpt->description, ",\"");
-    wpt->description = csv_stringtrim(wpt->description, "");
-
     fprintf(file_out, "%08.5f, %08.5f, \"%s\", \"%s\", \"%s\", %s, %d\n",
            wpt->position.latitude.degrees, wpt->position.longitude.degrees,
-           wpt->description, wpt->shortname, wpt->description, 
+           description, shortname, description, 
            color_hex, icon);
+
+    if (description)
+       free(description);
+    if (shortname)
+       free(shortname);
 }
 
 static void 
 data_write(void)
 {
-    waypt_disp_all(mxf_disp);
+    waypt_disp_all(mxf_waypt_pr);
 }
 
 ff_vecs_t mxf_vecs = {
index e64a69a39c2f5a0f43a11d98dfc4dfc5a00c5174..7fb507314a0a5ddf67f413df1db477f80250dd03 100644 (file)
@@ -95,8 +95,7 @@ data_read(void)
                    break;
                case 1:
                    /* waypoint name */
-                   wpt_tmp->shortname = xstrdup(s);
-                   wpt_tmp->shortname = csv_stringtrim(wpt_tmp->shortname, "");
+                   wpt_tmp->shortname = csv_stringtrim(s, "");
                    break;
                case 2:
                    /* degrees latitude */
@@ -127,8 +126,7 @@ data_read(void)
                    break;
                case 10:
                    /* Description */
-                   wpt_tmp->description = xstrdup(s);
-                   wpt_tmp->description = csv_stringtrim(wpt_tmp->description, "");
+                   wpt_tmp->description = csv_stringtrim(s, "");
 
                    break;
                case 11:
@@ -174,24 +172,36 @@ data_read(void)
 }
 
 static void 
-ozi_disp(waypoint * wpt)
+ozi_waypt_pr(const waypoint * wpt)
 {
     static int index = 0;
     double alt_feet;
     double ozi_time;
+    char * description;
+    char * shortname;
 
     ozi_time = (wpt->creation_time / 86400.0) + 25569.0;
     alt_feet = (wpt->position.altitude.altitude_meters * 3.2808); 
     
-    csv_stringclean(wpt->description, ",");
-    csv_stringclean(wpt->shortname, ",");
+    if (wpt->description) 
+        description = csv_stringclean(wpt->description, ",");
+    else
+        description = xstrdup("");
+        
+    if (wpt->shortname) 
+        shortname = csv_stringclean(wpt->shortname, ",");
+    else 
+        shortname = xstrdup("");
 
     index++;
 
     fprintf(file_out, "%4d,%-14.14s,%11.6f,%11.6f,%011.5f,%3d,%2d,%2d,%10d,%10d,%-40.40s,%2d,%2d,%5d,%7.0f,%2d,%2d,%2d\n", 
-       index, wpt->shortname, wpt->position.latitude.degrees, 
+       index, shortname, wpt->position.latitude.degrees, 
        wpt->position.longitude.degrees, ozi_time, 0, 1, 3, 0, 65535,
-       wpt->description, 0, 0, 0, alt_feet, 6, 0, 17);
+       description, 0, 0, 0, alt_feet, 6, 0, 17);
+
+    free(description);
+    free(shortname);
 
 }
 
@@ -204,7 +214,7 @@ data_write(void)
     fprintf(file_out, "Reserved 2\n");
     fprintf(file_out, "Reserved 3\n");
 
-    waypt_disp_all(ozi_disp);
+    waypt_disp_all(ozi_waypt_pr);
 }
 
 ff_vecs_t ozi_vecs = {
index 32941e394a60e7a6ddf8d3e4042bbd17347a990b..73c5bce78f22518b6caa5e637c23f3ce1340a563 100644 (file)
@@ -233,24 +233,34 @@ psp_read(void)
 }
 
 static void
-psp_disp(waypoint *wpt)
+psp_waypt_pr(const waypoint *wpt)
 {
        double lon, lat;
        char tbuf[64];
        char c;
        int i;
-
-        /* this output format pretty much requires a description and a shortname */
-        if (!wpt->shortname)  {
-            if (wpt->description)
-                wpt->shortname = xstrdup(wpt->description);
+       char *shortname;
+       char *description;
+       
+
+        /* this output format pretty much requires a description
+         * and a shortname 
+         */
+
+        if (wpt->shortname)  {
+            shortname = xstrdup(wpt->shortname);
+        } else {
+            if (wpt->description) 
+                shortname = xstrdup(wpt->description);
             else 
-                wpt->shortname = xstrdup("");
-        }
-        if (!wpt->description)  {
-            wpt->description = xstrdup(wpt->shortname);
+                shortname = xstrdup("");
         }
-        
+                
+        if (wpt->description)  {
+            description = xstrdup(wpt->description);
+        } else {
+            description = xstrdup(shortname);
+       }        
 
         /* convert lat/long back to radians */
        lat = (wpt->position.latitude.degrees * M_PI) / 180.0;
@@ -288,21 +298,21 @@ psp_disp(waypoint *wpt)
         /* 3 unknown bytes */
         fwrite(&tbuf, 1, 3, psp_file_out); /* 3 junk */
 
-        c = strlen(wpt->shortname);
+        c = strlen(shortname);
         /* 1 string size */
         fwrite(&c, 1, 1, psp_file_out);
 
-        for (i = 0 ; wpt->shortname[i] ; i++) {
-             fwrite(&wpt->shortname[i], 1, 1, psp_file_out);    /* char */
+        for (i = 0 ; shortname[i] ; i++) {
+             fwrite(&shortname[i], 1, 1, psp_file_out);    /* char */
              fwrite(&tbuf[0], 1, 1, psp_file_out);              /* null */
         }
 
-        c = strlen(wpt->description);
+        c = strlen(description);
         /*  1 byte string size */
         fwrite(&c, 1, 1, psp_file_out);
 
-        for (i = 0 ; wpt->description[i] ; i++) {
-             fwrite(&wpt->description[i], 1, 1, psp_file_out);  /* char */
+        for (i = 0 ; description[i] ; i++) {
+             fwrite(&description[i], 1, 1, psp_file_out);  /* char */
              fwrite(&tbuf[0], 1, 1, psp_file_out);              /* null */
         }
 
@@ -315,6 +325,9 @@ psp_disp(waypoint *wpt)
              fwrite(&tbuf[i], 1, 1, psp_file_out);              /* char */
              fwrite(&tbuf[0], 1, 1, psp_file_out);              /* null */
         }
+        
+        free (shortname);
+        free (description);
 }
 
 static void
@@ -338,7 +351,7 @@ psp_write(void)
 
         fwrite(&header_bytes, 1,  32, psp_file_out);
 
-        waypt_disp_all(psp_disp);
+        waypt_disp_all(psp_waypt_pr);
 }
 
 ff_vecs_t psp_vecs = {